<template>
  <div>
    <el-input v-model="keyword" placeholder="请输入你要查看的城市" class="search"></el-input>
    <div class="search-content" v-show="keyword">
      <ul>
        <li v-for="item of list" :key="item.id" @click="handleCityClidk(item.name)">{{ item.name }}</li>
        <li v-show="!list.length">暂无数据！</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CitySearch',
  props: {
    cities: Object
  },
  data () {
    return {
      keyword: '',
      list: []
    }
  },
  methods: {
    handleCityClidk (city) {
      this.$store.commit('changeCity', city)
      this.$router.push({ path: '/' })
    }
  },
  watch: {
    keyword () {
      const result = []
      if (!this.keyword) {
        this.list = []
        return
      }
      for (let i in this.cities) {
        // i就是字母ABCD...
        this.cities[i].forEach((val) => {
          if (val.spell.indexOf(this.keyword) > -1 || val.name.indexOf(this.keyword) > -1) {
            result.push(val)
          }
        })
      }
      this.list = result
    }
  }
}
</script>

<style lang="stylus" scoped>
.search >>> .el-input__inner
  text-align: center
.search-content
  position: absolute
  top: 86px
  left: 0
  right: 0
  bottom: 0
  background: #eee
  overflow: hidden
  z-index: 1
  li
    background: #fff
    color: #333
    padding: 8px 20px
    font-size: 14px
    border-bottom: 1px solid #dfdfdf
</style>
